package com.lucky.xyt.modular.system.service.impl;

import static com.lucky.xyt.core.common.constant.factory.MutiStrFactory.MUTI_STR_KEY;
import static com.lucky.xyt.core.common.constant.factory.MutiStrFactory.MUTI_STR_VALUE;
import static com.lucky.xyt.core.common.constant.factory.MutiStrFactory.parseKeyValue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lucky.xyt.core.cache.EhcacheFactory;
import com.lucky.xyt.core.common.constant.cache.Cache;
import com.lucky.xyt.core.common.exception.BizExceptionEnum;
import com.lucky.xyt.core.exception.GunsException;
import com.lucky.xyt.modular.system.dao.DictMapper;
import com.lucky.xyt.modular.system.model.Dict;
import com.lucky.xyt.modular.system.service.IDictService;

@Service
@Transactional
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

	@Resource
	private DictMapper dictMapper;

	@Override
	public void addDict(String dictName, String dictType, String dictValues, String dictTips) {

		// 判断有没有该字典
		List<Dict> dicts = dictMapper.selectList(new EntityWrapper<Dict>().eq("name", dictName).and().eq("pid", 0));
		if (dicts != null && dicts.size() > 0) {
			throw new GunsException(BizExceptionEnum.DICT_EXISTED);
		}

		// 解析dictValues
		List<Map<String, String>> items = parseKeyValue(dictValues);

		// 添加字典
		Dict dict = new Dict();
		dict.setName(dictName);
		dict.setType(dictType);
		dict.setTips(dictTips);
		dict.setNum(0);
		dict.setPid(0);
		this.dictMapper.insert(dict);

		// 添加字典条目
		Map map = EhcacheFactory.getInstance().get(Cache.DICTMAP, dictType);
		if(map == null) map =new HashMap();
		for (Map<String, String> item : items) {
			String num = item.get(MUTI_STR_KEY);
			String name = item.get(MUTI_STR_VALUE);
			Dict itemDict = new Dict();
			itemDict.setPid(dict.getId());
			itemDict.setName(name);
			try {
				itemDict.setNum(Integer.valueOf(num));
			} catch (NumberFormatException e) {
				throw new GunsException(BizExceptionEnum.DICT_MUST_BE_NUMBER);
			}
			this.dictMapper.insert(itemDict);

			map.put(num, name);
		}

		EhcacheFactory.getInstance().put(Cache.DICTMAP, dictType, map);

	}

	@Override
	public void editDict(Integer dictId, String dictName, String dictType, String dicts, String dictTips) {
		// 删除之前的字典
		this.deleteDict(dictId);

		// 重新添加新的字典
		this.addDict(dictName, dictType, dicts, dictTips);

	}

	@Override
	public void deleteDict(Integer dictId) {
		// 删除这个字典的子词典
		Wrapper<Dict> dictEntityWrapper = new EntityWrapper<>();
		dictEntityWrapper = dictEntityWrapper.eq("pid", dictId);
		dictMapper.delete(dictEntityWrapper);

		// 删除这个词典
		dictMapper.deleteById(dictId);
	}

	@Override
	public List<Dict> selectByCode(String code) {
		return this.baseMapper.selectByCode(code);
	}

	@Override
	public List<Map<String, Object>> list(Map<String, String> condition) {
		return this.baseMapper.list(condition);
	}

	@Override
	public List<Dict> queryAllDict() {
		  Wrapper<Dict> dictEntityWrapper = new EntityWrapper<>();
	        dictEntityWrapper = dictEntityWrapper.eq("pid", 0);
	       return  dictMapper.selectList(dictEntityWrapper);
	}
}
